package com.huang825172.sbog.Tabs.Mine.MinePage.View;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huang825172.sbog.BaseFlux.Dispatcher;
import com.huang825172.sbog.BaseFlux.Store.IDataChangedListener;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Create.SelectPage.View.SelectPage;
import com.huang825172.sbog.Tabs.Mine.InfoEditPage.View.InfoEditPage;
import com.huang825172.sbog.Tabs.Mine.LoginPage.Store.Datas.Login;
import com.huang825172.sbog.Tabs.Mine.LoginPage.View.LoginPage;
import com.huang825172.sbog.Tabs.Mine.MinePage.Action.ActionCreator;
import com.huang825172.sbog.Tabs.Mine.MinePage.Store.Interfaces;
import com.huang825172.sbog.Tabs.Mine.MinePage.Store.Store;
import com.huang825172.sbog.Tabs.Mine.MinePage.Store.ViewDatas.VideoListItem;
import com.huang825172.sbog.Tabs.Mine.RegisterPage.View.RegisterPage;
import com.huang825172.sbog.Util.TAGs;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;


public class MinePage extends Fragment implements View.OnClickListener, IDataChangedListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    @BindView(R.id.MinePageVideosRecyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.MinePagePhotoCircleImageView)
    CircleImageView photoImageView;
    @BindView(R.id.MinePageUIDTextView)
    TextView uidTextView;
    @BindView(R.id.MinePageAgeTextView)
    TextView ageTextView;
    @BindView(R.id.MinePageAreaTextView)
    TextView areaTextView;
    @BindView(R.id.MinePageIntroTextView)
    TextView introTextView;
    @BindView(R.id.MinePageLikeNumTextView)
    TextView likeNumTextView;
    @BindView(R.id.MinePageFollowNumTextView)
    TextView followNumTextView;
    @BindView(R.id.MinePageFansNumTextView)
    TextView fansNumTextView;

    @BindView(R.id.MinePageToolBarLayout)
    CollapsingToolbarLayout toolbarLayout;

    @BindView(R.id.MinePageEditProfileContainer)
    LinearLayout editProfileContainer;

    @BindView(R.id.MinePageBackgroundContainer)
    RelativeLayout backgroundContainer;

    @BindView(R.id.MinePageQuitLoginTextView)
    TextView quitLoginTextView;

    @BindView(R.id.MinePagePublicContainer)
    RelativeLayout publicContainer;

    private Store store;
    private ActionCreator actionCreator;

    public MinePage() {
        // Required empty public constructor
    }

    public static MinePage newInstance(String param1, String param2) {
        MinePage fragment = new MinePage();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine_page, container, false);
        ButterKnife.bind(this, view);

        store = new Store(getContext());
        actionCreator = new ActionCreator();
        Dispatcher.getSingleton().register(store);
        store.setDataChangedListener(this);

        actionCreator.createRefleshAction();

        List<VideoListItem> Data = new ArrayList<>();
        Data.add(new VideoListItem());
        Data.add(new VideoListItem());
        Data.add(new VideoListItem());
        Data.add(new VideoListItem());
        Data.add(new VideoListItem());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(new MinePageRecyclerViewAdapter(Data));

        photoImageView.setOnClickListener(this);
        editProfileContainer.setVisibility(View.INVISIBLE);
        quitLoginTextView.setVisibility(View.INVISIBLE);
        publicContainer.setVisibility(View.INVISIBLE);

        return view;
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.MinePagePhotoCircleImageView: {
                Intent intent = new Intent();
                intent.setClass(getContext(), LoginPage.class);
                startActivity(intent);
                break;
            }
            case R.id.MinePageEditProfileContainer: {
                Intent intent = new Intent();
                intent.setClass(getContext(), InfoEditPage.class);
                startActivity(intent);
                break;
            }
            case R.id.MinePageQuitLoginTextView:{
                actionCreator.createQuitLoginAction();
                break;
            }
            case R.id.MinePagePublicContainer:{
                Intent intent = new Intent();
                intent.setClass(getContext(), SelectPage.class);
                startActivity(intent);
                break;
            }
            default:
                break;
        }
    }

    @Override
    public void onDataChanged(String label) {
        switch (label) {
            case "MinePageRefleshAction": {
                break;
            }
            case "GlobalManagerCheckTokenAction": {
                Toast.makeText(getContext(), "Token Status = " + store.getTokenStatus(), Toast.LENGTH_SHORT).show();
                break;
            }
            case "MinePageStatusUnlogined": {
                photoImageView.setOnClickListener(this);
                break;
            }
            case "MinePageStatusLogined": {
                photoImageView.setOnClickListener(null);
                photoImageView.setImageResource(R.mipmap.login_background);
                uidTextView.setText(getContext().getResources().getString(R.string.UID) + ":" + store.getUID());
                toolbarLayout.setTitle(store.getUsername());

                ageTextView.setText("18岁");
                areaTextView.setText("广东-广州");

                likeNumTextView.setText("0 点赞");
                followNumTextView.setText("0 关注");
                fansNumTextView.setText("0 粉丝");

                editProfileContainer.setVisibility(View.VISIBLE);
                editProfileContainer.setOnClickListener(this);

                quitLoginTextView.setVisibility(View.VISIBLE);
                quitLoginTextView.setOnClickListener(this);

                publicContainer.setVisibility(View.VISIBLE);
                publicContainer.setOnClickListener(this);

                backgroundContainer.setBackgroundResource(R.mipmap.register_background);
                break;
            }
            case "MinePageQuitLoginAction":{
                photoImageView.setOnClickListener(this);
                photoImageView.setImageResource(R.mipmap.main_logo);
                uidTextView.setText("");
                toolbarLayout.setTitle("");

                likeNumTextView.setText("点赞");
                followNumTextView.setText("关注");
                fansNumTextView.setText("粉丝");

                editProfileContainer.setVisibility(View.INVISIBLE);
                editProfileContainer.setOnClickListener(null);

                quitLoginTextView.setVisibility(View.INVISIBLE);
                quitLoginTextView.setOnClickListener(null);

                publicContainer.setVisibility(View.INVISIBLE);
                publicContainer.setOnClickListener(null);

                backgroundContainer.setBackgroundColor(getContext().getResources().getColor(R.color.lighterGray));
                break;
            }
            default:
                break;
        }
    }
}
